<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>列表</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"
          name="viewport">
    <link href="../../layuiadmin/layui/css/layui.css" media="all" rel="stylesheet">
    <link href="../../layuiadmin/style/admin.css" media="all" rel="stylesheet">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <!-- 头部内容 -->
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input autocomplete="off" class="layui-input" id="entityName" placeholder="订单号/联系人/电话"
                               type="text">
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" id="entity_search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <!-- <button class="layui-btn layuiadmin-btn-role" data-type="add">新增预定</button>-->
                </div>
            </div>
        </div>
        <!-- 数据表格 -->
        <div class="layui-card-body">
            <table id="entityTable" lay-filter="entityTable"></table>
        </div>
    </div>
</div>

<!-- 表格每一行的操作栏显示的内容 -->
<script id="table_bar" type="text/html">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    </div>
</script>

<script src="../../js/jquery-1.8.3.min.js"></script>
<script src="../../layuiadmin/layui/layui.js"></script>
<!----------------------- 初始化配置 begin ------------------------>
<script>
    /*********路径配置 - 手动填写**********/
        //当前订单页
    var findPageURL = "/room_predict_order/findPage";
    //加载详情模态窗初始化页面所需的数据
    var toAddURL = "/room_predict_order/toAdd";
    //作废当前模态窗中的单号
    var closeModelURL = "/room_predict_order/close";
    //添加模态窗中的订单详情
    var addURL = "/room_predict_order/add";
    //查看选中行订单的订单信息
    var toUpdateURL = "/room_predict_order/findOne";
    //查看选中行订单的详细信息
    var toDetailsURL = "/room_predict_order/findDetails";
    //修改订单信息的url
    var updateURL = "/room_predict_order/update";
    //删除订单信息的url
    var deleteURL = "/room_predict_order/delete";
    /***************路径配置结束*******************/
        // 弹出层的表头文本
    var formTitle = "客房预定单";
    //页面配置 - 不用填写
    var entityType = "";
    var entityURL = "";
    var entityId = "";
    // -------------------------- 初始化配置 end ---------------------------

    /**
     * 数据表格id:entityTable
     * 默认搜索框id：entityName
     * 搜索按钮id:entity_search
     *
     */

    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table', 'util'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , layer = layui.layer
            , util = layui.util
            , index = layui.index;

        //监听头部搜索按钮
        $("#entity_search").on('click', function (data) {
            //执行表格重载
            table.reload('entityTable', {
                method: 'post',
                where: {
                    "orderNumber": $("#entityName").val(),
                },
                page: {curr: 1}
            });
        });

        //数据表格执行渲染 cols为数据列
        table.render({
            elem: '#entityTable'
            , id: 'entityTable'
            , url: findPageURL
            , toolbar: true
            , totalRow: true
            , page: true
            , limits: [10, 20, 30, 50]
            , cols: [[
                {fixed: 'left', field: 'ordernumber', title: '订单号', width: 160, sort: true, totalRowText: "合计"}
                , {field: 'realname', title: '联系人', width: 90, align: "center"}
                , {field: 'phone', title: '电话', width: 130}
                , {
                    field: 'predict_in_time', title: '预订入住时间', width: 130, sort: true,
                    templet: function (d) {
                        return util.toDateString(d.predict_in_time, 'yyyy-MM-dd')
                    }
                }
                , {
                    field: 'predict_out_time', title: '预订退房时间', width: 120,
                    templet: function (d) {
                        return util.toDateString(d.predict_out_time, 'yyyy-MM-dd')
                    }
                }
                , {field: 'days', title: '天数', width: 90, align: "center"}
                , {
                    field: 'is_status', title: '状态', width: 90, align: "center", templet: function (d) {
                        if (d.is_status == "0") return "<span class=\"layui-badge layui-bg-green\" >未入住</span>\n"
                        else if (d.is_status == "1") return "<span class=\"layui-badge\" >已入住</span>\n"
                        else if (d.is_status == "9") return "<span class=\"layui-badge\" >已取消</span>\n"
                    }
                }
                , {field: 'totalprice', title: '总价', width: 90, align: "center", totalRow: true}
                , {
                    field: 'gmt_create', title: '创建时间', width: 130, sort: true,
                    templet: function (d) {
                        return util.toDateString(d.gmt_create, 'yyyy-MM-dd HH:mm:ss')
                    }
                }
                // , {
                //     field: 'gmt_modified', title: '修改时间', width: 130, sort: true,
                //     templet: function (d) {
                //         return util.toDateString(d.gmt_modified, 'yyyy-MM-dd HH:mm:ss')
                //     }
                // }
                , {
                    fixed: 'right', title: '操作', width: 155, align: 'center', templet: function (d) {
                        var html = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>';
                        var PTime = new Date(d.predict_in_time);
                        var NowTime = new Date();
                        NowTime = new Date(NowTime.getFullYear() + "-" + (NowTime.getMonth() + 1) + "-" + NowTime.getDate());
                        PTime.setDate(PTime.getDate() + 1);
                        if (PTime > NowTime && d.is_status == 0) {
                            html += '<a class="layui-btn layui-btn-xs layui-bg-red" lay-event="del">取消</a>'
                        }
                        return html;
                    }
                }
            ]]
            , where: {
                "orderNumber": $("#entityName").val()
            }
        });


        //添加操作，点击添加按钮触发的事件,修改全局变量
        var $ = layui.$, active = {
            add: function () {
                entityType = "add";
                entityURL = addURL;
                var layerAdd = layer.open({
                    type: 2
                    , title: false
                    , content: 'form.html'
                    , resize: false
                    , scrollbar: false
                    , area: ['850px', '500px']
                    , btn: false
                    , closeBtn: 0
                    , end: function () {
                        //当该窗体销毁时将全局变量恢复默认值
                        entityType = "";
                        entityURL = "";
                    }
                });
                //打开该窗体默认占满屏幕
                layer.full(layerAdd);
            }
        };

        //监听数据表格行工具事件
        table.on('tool(entityTable)', function (obj) { //注：tool 是工具条事件名，tool()中的是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr;    //获得当前行 tr 的DOM对象
            //根据id=table_bar内的内容进行分情况
            switch (layEvent) {
                case 'del':
                    //点击作废按钮触发的事件
                    layer.confirm('您确定将本订单退款吗？', function (index) {
                        var load = layer.load(2);
                        //向服务端发送删除指令
                        $.post(deleteURL, {id: data.id}, function (data) {
                            layer.close(load);
                            //得到响应后弹窗提醒
                            layer.msg(data.msg, {icon: data.icon, time: 1500});
                            //data.icon为1的时候说明作废成功
                            if (data.icon == 1) {
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                $(".layui-laypage-btn").click();//点击分页刷新当前页
                            }
                        });
                    });
                    break;
                case 'edit':
                    entityType = "toUpdate";
                    entityURL = updateURL;
                    entityId = data.id;
                    layer.open({
                        type: 2
                        , title: "修改" + formTitle + "信息"
                        , content: 'form.html'
                        , maxmin: true
                        , area: ['400px', '500px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#entity-submit");
                            submit.click();
                        }
                    });
                    break;
                case 'detail':
                    //点击详情按钮触发的事件
                    entityType = "toDetails";
                    entityURL = updateURL;
                    entityId = data.id;
                    var layerDetail = layer.open({
                        type: 2
                        , title: "查看" + formTitle
                        , content: 'form.html'
                        , resize: false
                        , area: ['850px', '500px']
                        , scrollbar: false
                        , btn: ['关闭']
                        , end: function () {
                            //当该窗体销毁时将全局变量恢复默认值
                            entityURL = "";
                            entityType = "";
                        }
                    });
                    //打开该窗体默认占满屏幕
                    layer.full(layerDetail);
                    break;
            }
        });
        //必备-事件转发器
        $('.layui-btn.layuiadmin-btn-role').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

